﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface; 

namespace MaxStudio.Concrete.Server.Service
{
    public class UserService : ServiceHandle, IUsers 
    {
        #region IUsers Members

        public OperationResult<List<tb_User>> GetAllUserList()
        {
            ConcreteDaoDataContext dataContext = getDateContext();

            OperationResult<List<tb_User>> result = new OperationResult<List<tb_User>>();
            List<tb_User> list = new List<tb_User>();
            try
            {
                list = dataContext.GetAllUserList(1, 99999);

                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }


        public OperationResult<List<tb_User>> GetAllUserList(int currentPage, int PageSize)
        { 
            ConcreteDaoDataContext dataContext = getDateContext(); 

            OperationResult<List<tb_User>> result = new OperationResult<List<tb_User>>();
            List<tb_User> list = new List<tb_User>();
            try
            {
                 list = dataContext.GetAllUserList(currentPage,PageSize );
                
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_User> GetUserById(int id)
        {
            ConcreteDaoDataContext dataContext = getDateContext();
            OperationResult<tb_User> result = new OperationResult<tb_User>();

            try
            { 
                tb_User user = dataContext.GetUserById(id);  
                if (user == null)
                {
                    result.errorMessageKey = "No exits";
                }
                else
                {
                    result.obj = user;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }
            return result;
        }

        public OperationResult<tb_User> CreateUser(tb_User entityUser)
        {
            OperationResult<tb_User> result = new OperationResult<tb_User>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            { 
                dataContext.CreateUser(entityUser);
                dataContext.SubmitChanges();
                result.obj = entityUser;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_User> ModifyUser(tb_User instance)
        {
            OperationResult<tb_User> result = new OperationResult<tb_User>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {  
                dataContext.ModifyUser(instance.Id, instance.IsDelete, instance.Name, instance.PWD, instance.Tel, instance.UserGroup, instance.tb_FunctionGroup);
                dataContext.SubmitChanges();
                result.obj = instance;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_User> DeleteUser(tb_User instance)
        {
            OperationResult<tb_User> result = new OperationResult<tb_User>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.DeleteUser(instance.Id);
                dataContext.SubmitChanges();
                result.obj = instance;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<List<tb_User>> GetUserByGroup(int groupId)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
